package com.CST.service.impl.cb;

import com.CST.common.api.Response;
import com.CST.common.constant.Constants;
import com.CST.entity.cb.RuleCompelamortization;
import com.CST.entity.cb.selectInfo;
import com.CST.mapper.cst.cb.RuleCompelamortizationMapper;
import com.CST.service.cb.IRuleCompelamortizationService;
import com.CST.util.HttpUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-10-27
 */
@Service
public class RuleCompelamortizationServiceImpl extends ServiceImpl<RuleCompelamortizationMapper, RuleCompelamortization> implements IRuleCompelamortizationService {


    @Override
    public List<RuleCompelamortization> getRuleCompelAmortization(RuleCompelamortization entity) {
        return baseMapper.getRuleCompelAmortization(entity);
    }

    @Override
    public RuleCompelamortization getRuleCompelAmortizationById(RuleCompelamortization entity) {
        return baseMapper.getRuleCompelAmortizationById(entity);
    }

    @Override
    public Response batchDeleteRuleCompelAmortization(List<Integer> ids) {
        try {
            baseMapper.batchDeleteRuleCompelAmortization(ids);
        } catch (Exception ex) {
            return Response.error("删除失败");
        }
        return Response.success("删除成功");
    }

    @Override
    public Response addRuleCompelAmortization(RuleCompelamortization entity) {
        try {
            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:hh:ss");
            entity.setExtendField1(entity.getYearTarget().toString());
            entity.setExtendField2(entity.getMonthTarget().toString());
            entity.setCreatedOn(sdf.format(currDate));
            entity.setUpdatedOn(sdf.format(currDate));
            String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
            userName = userName == null || userName.isEmpty() ? "System" : userName;
            entity.setCreatedBy(userName);
            entity.setUpdatedBy(userName);
            baseMapper.addRuleCompelAmortization(entity);
        } catch (Exception ex) {
            return Response.error(String.join(":", "添加失败:", ex.getMessage()));
        }
        return Response.success("添加成功");
    }

    @Override
    public Response insertBatchRuleCompelAmortization(RuleCompelamortization entity) {
        try {
            RuleCompelamortization searchInfo = new RuleCompelamortization();
            searchInfo.setYearSource(entity.getYearSource());
            searchInfo.setMonthSource(entity.getMonthSource());
            List<RuleCompelamortization> sourceList =baseMapper.getRuleCompelAmortization(searchInfo);
            if (CollectionUtils.isEmpty(sourceList)) {
                return Response.error("继承失败,没有找到对应规则下的数据");
            }

            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //清除当前月记录
            baseMapper.deleteByYearMonth(entity.getYearTarget(),entity.getMonthTarget());
            int total = sourceList.size();//为防止插入字符超过最大数,分批插入
            int fromIndex = 0;
            List<RuleCompelamortization> insertList = new ArrayList<>();
            for (int i = 0; i < sourceList.size(); i++) {
                RuleCompelamortization item = sourceList.get(i);
                item.setExtendField1(String.valueOf(entity.getYearTarget()));
                item.setExtendField2(String.valueOf(entity.getMonthTarget()));
                item.setCreatedOn(sdf.format(currDate));
                item.setUpdatedOn(sdf.format(currDate));
                String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
                userName = userName == null || userName.isEmpty() ? "System" : userName;
                item.setCreatedBy(userName);
                item.setUpdatedBy(userName);
                fromIndex++;
                insertList.add(item);
                if (fromIndex % 20 == 0 && fromIndex <= total) {
                    baseMapper.insertBatchRuleCompelAmortization(insertList);
                    insertList = new ArrayList<>();
                }
            }
            if (CollectionUtils.isNotEmpty(insertList)) {
                baseMapper.insertBatchRuleCompelAmortization(insertList);
            }

        } catch (Exception ex) {
            return Response.error(String.join(":", "继承失败:", ex.getMessage()));
        }
        return Response.success("继承成功");
    }

    @Override
    public List<selectInfo> getYearMonth() {
        return baseMapper.getYearMonth();
    }

    @Override
    public Response modifyRuleCompelAmortization(RuleCompelamortization entity) {
        if (entity.getId() == 0) {
            return Response.error("主键不能为空");
        }
        try {
            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:hh:ss");
            Calendar cal =Calendar.getInstance();
            entity.setUpdatedOn(sdf.format(currDate));
            String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
            userName = userName == null || userName.isEmpty() ? "System" : userName;
            entity.setUpdatedBy(userName);
            baseMapper.modifyRuleCompelAmortization(entity);
        } catch (Exception ex) {
            return Response.error(String.join(":", "修改失败", ex.getMessage()));
        }
        return Response.success("修改成功");
    }
}
